<template>
  <div id="add">
    <el-dialog
      :title="obj.isadd ? '分类添加' : '分类修改'"
      :visible.sync="obj.istrue"
      width="50%"
      @close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 45%; background: #fff"
      >
        <el-form-item label="上级菜单">
          <el-select
            v-model="form.pid"
            placeholder="请选择"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in catelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" >
          <el-input v-model="form.catename" ></el-input>
        </el-form-item>
       <el-form-item label="上传图片">
          <div class="filebox">
            <h2>+</h2>
            <img :src="imgURL" alt="" v-if="imgURL">
            <input type="file" @change="changeimg">
          </div>
       </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitadd" v-if="obj.isadd"
          >确 定</el-button
        >
        <el-button type="primary" v-else @click="edit">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { successAlert, warningAlert } from "../../../util/alert.js";
import { mapActions, mapGetters } from "vuex";
import { cateadd, cateinfo, cateedit } from "../../../requset/api";
export default {
  props: ["obj"],
  data() {
    return {
      imgURL:"",
      form: {
        pid: 0,
        catename:"",
        status: 1,
        img:""
      },
    };
  },
  computed: {
    ...mapGetters({
      catelist: "cate/list",
    }),
  },
  methods: {
    ...mapActions({
      reqChangeCate: "cate/reqChangeCate",
    }),
    emty() {
      this.form = {
      pid: 0,
        catename:"",
        status: 1,
        img:""
      };
      this.imgURL=""
    },
    cancel() {
      (this.obj.istrue = false), (this.obj.isadd = true), this.emty();
    },
    submitadd() {
      console.log(this.form);
      cateadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeCate();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getone(id) {
      cateinfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.imgURL=this.$pre+res.data.list.img
        this.form.id = id;
      });
    },
    edit() {
      cateedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeCate();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    changeimg(e){
      let file=e.target.files[0]
      if(file.size>=2*1024*1024){
        warningAlert("图片超出规定的大小，请上传小于2M的图片")
      }else{
        this.imgURL=URL.createObjectURL(file)
        this.form.img=file
      }
    }
  },
};
</script>

<style lang="less" scoped>
  .filebox{
    height: 200px;
    width: 200px;
    border: 1px dashed #cecece;
    position: relative;
    h2{
      line-height: 200px;
      font-size: 30px;
      font-weight: bold;
      text-align: center;
    }
    input{
      height: 200px;
      width: 200px;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
    }
    img{
      height: 200px;
      width: 200px;
      position: absolute;
      left: 0;
      top: 0;
    }
  }

</style>